<?php if(!defined('BASEPATH')) exit('No direct script access allowed!');

class Classes_Model extends MY_Model {
	protected $_table_name = 'classes';

	public function __construct() {
		parent::__construct();
	}	
	
	public function get_by_name($name) {
		$this->db->where('name',$name);
		$rs = $this->db->get($this->_table_name);
		$rs = $rs->result_array();
		return $rs[0];
	}
	
	public function get_current_school_year_id(){
		$ci =&get_instance();
		$ci->load->model('school_years_model','school_years');
		$sy = $ci->school_years->get_active_school_year();  
		return $sy[0]['id'];
	}
	
	public function update($primary_key) {
		$ci =&get_instance();
		$ci->load->model('school_years_model','school_years');
		$sy = $ci->school_years->get_active_school_year();  
		
		$data['school_year_id'] = $sy['id'];
		$this->db->update($this->_table_name, $data, array('id'=>$primary_key));
		echo $this->db->last_query();
	}
	
	public function lists_array(){
		$rs = $this->db->get($this->_table_name)->result_array();
		$data = array();
		$data[0] = '';
		foreach($rs as $row){
			$data[ $row['id'] ] = $row['name'];
		}
		return $data;
	}
	
	public function questions_lists_array($school_year_id, $teacher_id, $subject_id){
		$this->db->where('school_year_id',$school_year_id);
		$this->db->where('teacher_id',$teacher_id);
		$this->db->where('subject_id',$subject_id);
		$rs = $this->db->get('questions')->result_array();
		
		$data = array();
		$data[0] = '';
		foreach($rs as $row){
			$data[ $row['id'] ] = 'Bank Soal ' . $row['chapter_name'];
		}
		return $data;
	}
	
	public function simple_lists($school_year_id){		
		$this->db->where('school_year_id', $school_year_id);
		$rs = $this->db->get($this->_table_name);
		return $rs->result_array();
	}
}
